(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.notifydoc.editPopupController', notifydocController);
	notifydocController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 
	                                'validatorConfig', '$filter', 'authService'];
	function notifydocController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, $filter, authService) {
		var ctrl = this, params, oldEventNotifyTpl = {}, oldEventNotifyTpls = [];
		ctrl.dialogsModule = dialogsModule;
		var params = ctrl.dialogsModule.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = params.validation;
		
		ctrl.ok = function() {
			if($scope.notifyDocForm) {
				$scope.notifyDocForm.$setSubmitted();
				if(!$scope.notifyDocForm.$valid) {
					dialogsModule.showErrorMessages($scope.notifyDocForm);
					return;
				}
			}
			
			var notifyTpl = ctrl.notifyTpl;
			if (orgNotifyTitle != notifyTpl.notifyTitle || orgNotifyContent != notifyTpl.notifyContent) {
				notifyTpl.dirty = true;
			}
			if (notifyTpl.overrideFlag && notifyTpl.overrideFlag == 'Y' && (notifyTpl.notifyTitle == null || notifyTpl.notifyTitle.length == 0)) {
				modalService.openErrorDialogs({
					messages : ["js.info.notify.title.required"]
				});
				return false;
			}
			if (notifyTpl.overrideFlag && notifyTpl.overrideFlag == 'Y' && (notifyTpl.notifyContent == null || notifyTpl.notifyContent.length == 0)) {
				modalService.openErrorDialogs({
					messages : ["js.info.notify.content.required"]
				});		
				return false;
			}			
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](angular.copy(ctrl.notifyTpl)) !== false) {
				$uibModalInstance.close();
			}
		};		
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		var orgNotifyTitle = '', orgNotifyContent = '';
		var orgLocaleCode, orgNotifyType, orgOverrideFlag;
		
		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.serviceEventNotifyTplParams';
			if (ctrl.dialogsModule.pageId == 'CDA_NOTIFYDOC_VIEWPOPUP') {
				ctrl.innerGridOptions.enableRowHeaderSelection = false;
				ctrl.ok = null;
			} 
			
			ctrl.notifyTpl = params.event||params.notifyTpls ||{}; 
			if (params.event != null ) {
				if (params.notifyTpls != null && params.notifyTpls.length >0 ){
					var count = 0;
					angular.forEach(params.notifyTpls, function (notifyNow, key) {
						if ($filter('encode')(notifyNow.eventCode) == $filter('encode')(params.event.eventCode)) {
							if(notifyNow.notifyTitle){
								ctrl.notifyTpl.overrideFlag = 'Y';
							} else {
								ctrl.notifyTpl.overrideFlag = notifyNow.overrideFlag;
							}
							ctrl.notifyTpl.localeCode = notifyNow.localeCode;
							ctrl.notifyTpl.notifyType = notifyNow.notifyType;
							ctrl.notifyTpl.notifyParty = notifyNow.notifyParty;		
							ctrl.localeLookupOptions = {data: [{label: $filter('encode')(notifyNow.localeCode), value: notifyNow.localeCode}]};
							ctrl.notifyTypeOptions =  {data: [{label: notifyNow.notifyType, value: notifyNow.notifyType}]};
							ctrl.notifyPartyOptions = {data: [{label: notifyNow.notifyParty, value: notifyNow.notifyParty}]};
							
							ctrl.notifyTpl.notifyTplId = notifyNow.notifyTplId;
							ctrl.notifyTpl.notifyTitle = notifyNow.notifyTitle;
							ctrl.notifyTpl.notifyContent = notifyNow.notifyContent;
							
							ctrl.notifyTpl.eventCode = params.event.eventCode;		
							ctrl.notifyTpl.eventName = params.event.eventName;
							ctrl.notifyTpl.siteCode = params.event.siteCode;
							return;
						} else {
							count ++;
						}
					});
					if(count == params.notifyTpls.length){
						getDefaultTplByEventCode();
					} 
				} else {
					getDefaultTplByEventCode();    
				}
			} 
			
			ctrl.resetNotifyTpl = function (notifyTpl, formCtrl, isInitialized){
				if( !formCtrl.$dirty){
					return;
				}
				ctrl.notifyTpl = notifyTpl;
				ctrl.notifyTpl.overrideFlag = 'N';
				ctrl.layoutConfig.formConfigs.general.gridConfigs['notifyTitle'].readonly = true;
				ctrl.layoutConfig.formConfigs.general.gridConfigs['notifyContent'].readonly = true;
				ctrl.notifyTpl.notifyTitle = null;
				ctrl.notifyTpl.notifyContent = null;
			};
			
			ctrl.resetNotifyTitle = function (notifyTpl,formCtrl){
				if( !formCtrl.$dirty){
					if("N" == ctrl.notifyTpl.overrideFlag){
						ctrl.layoutConfig.formConfigs.general.gridConfigs['notifyTitle'].readonly = true;
						ctrl.layoutConfig.formConfigs.general.gridConfigs['notifyContent'].readonly = true;
					}
					return;
				}
				ctrl.notifyTpl.overrideFlag = ctrl.notifyTpl.overrideFlag;
				if("Y" == ctrl.notifyTpl.overrideFlag){
					ctrl.layoutConfig.formConfigs.general.gridConfigs['notifyTitle'].readonly = false;
					ctrl.layoutConfig.formConfigs.general.gridConfigs['notifyContent'].readonly = false;
					ctrl.notifyTpl.notifyTitle = null;
					ctrl.notifyTpl.notifyContent = null;
				} else {
					ctrl.layoutConfig.formConfigs.general.gridConfigs['notifyTitle'].readonly = true;
					ctrl.layoutConfig.formConfigs.general.gridConfigs['notifyContent'].readonly = true;
					ctrl.notifyTpl.notifyTitle = null;
					ctrl.notifyTpl.notifyContent = null;
				}
			};
			
		};
		
		function getDefaultTplByEventCode (){
			ctrl.notifyTpl.overrideFlag = ctrl.notifyTpl.overrideFlag ||'N';
			authService.getEventNotifyTypeOption(params.event).then(function(response) {	//get by eventcode serviceEvent+
				ctrl.notifyTypeOptions = response; 
				if (ctrl.notifyTypeOptions.data && ctrl.notifyTypeOptions.data.length > 0) {
					ctrl.notifyTpl.notifyType = ctrl.notifyTypeOptions.data[0].value;
				}
			}).then(
				authService.getEventNotifyPartyOption(params.event).then(function(response) {	//get by eventcode TCDA_G_NOTIFYTPL
				ctrl.notifyPartyOptions = response; 
				if (ctrl.notifyPartyOptions.data && ctrl.notifyPartyOptions.data.length > 0) {
					ctrl.notifyTpl.notifyParty = ctrl.notifyPartyOptions.data[0].value;
				}
			})).then(
				authService.getLocalOptions(params.event).then(function(response) {				//get by eventcode
				ctrl.localeLookupOptions = response; 
				if (ctrl.localeLookupOptions.data && ctrl.localeLookupOptions.data.length > 0) {
					ctrl.notifyTpl.localeCode = ctrl.localeLookupOptions.data[0].value;
				}
			})).then(
				authService.getDefaultNotifyTplByEvent(params.event).then(function(response) {			//get by eventcode
					var defaultNotifyTplList = response.data;
					if(defaultNotifyTplList != null && defaultNotifyTplList.length >0){
						angular.forEach(defaultNotifyTplList, function(tpl, key){
							if($filter('encode')(tpl.localeCode) == $filter('encode')(params.event.localeCode)){
								
								ctrl.notifyTpl.notifyTitle = tpl.notifyTitle; 
								ctrl.notifyTpl.notifyContent = tpl.notifyContent; 
							}
						});
					}
				})	
			)		
		}
		
		ctrl.viewEventNotifyTplParam = function() {
			ctrl.notifyTpl = params.event||params.notifyTpls ||{}; 
			
			modalService.openCustomDialogs({
				'params' : ctrl.notifyTpl,
				'dialogsModule' : 'cda.service.serviceEventNotifyTplParam'
			});
		};
		ctrl.viewEventNotifyTpl = function() {
			modalService.openCustomDialogs({
				'params' : ctrl.notifyTpl,
				'dialogsModule' : 'cda.service.serviceEventNotifyTpl'
			});
		};
		
		
	}
})(angular);